package br.ufpe.cin.plebeia.webapp.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import br.ufpe.cin.plebeia.model.Usuario;
import br.ufpe.cin.plebeia.persistence.hibernateDao.HibernateUsuarioDao;
import br.ufpe.cin.plebeia.webapp.form.ListActionForm;
import br.ufpe.cin.plebeia.webapp.form.LoginActionForm;
import br.ufpe.cin.plebeia.webapp.form.UsuarioActionForm;
import br.ufpe.cin.plebeia.webapp.infra.PathDispatchAction;

public class UsuarioAction extends PathDispatchAction {
	private static final String JSP = "jsp";

	private HibernateUsuarioDao dao = new HibernateUsuarioDao();

	public ActionForward login(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse httpServletResponse) throws Exception {
		LoginActionForm form = (LoginActionForm) actionForm;

		Usuario usuario = dao.retrieveByLogin(form.getLogin());

		ActionErrors errors = new ActionErrors();

		if (usuario == null || !usuario.getSenha().equals(form.getSenha())) {
			errors.add("error",
					new ActionMessage("error.loginInvalido"));
			saveErrors(request.getSession(), errors);
			return actionMapping.getInputForward();
		}
		if (usuario.getPermissao() != 1) {
			errors.add("error",
					new ActionMessage("error.permissao"));
			saveErrors(request.getSession(), errors);
			return actionMapping.getInputForward();
		}
		return actionMapping.findForward("showList");
	}

	public ActionForward listarUsuario(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse) throws Exception {
		System.out.println("UsuarioAction.listarUsuario()");
		List<Usuario> usuarios = dao.retrieveAll();
		ListActionForm form = (ListActionForm) actionForm;
		form.setList(usuarios);
		return actionMapping.findForward(JSP);
	}

	public ActionForward exibirUsuario(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse) throws Exception {
		System.out.println("UsuarioAction.exibirUsuario()");
		String idUsuario = httpServletRequest.getParameter("idUsuario");
		Usuario usuario = dao.retrieve(Long.parseLong(idUsuario));
		UsuarioActionForm form = (UsuarioActionForm) actionForm;
		if (usuario == null)
			usuario = new Usuario();
		form.setUsuario(usuario);
		return actionMapping.findForward(JSP);
	}

	public ActionForward salvarUsuario(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse) throws Exception {
		System.out.println("UsuarioAction.salvarUsuario()");
		UsuarioActionForm form = (UsuarioActionForm) actionForm;
		dao.save(form.getUsuario());
		return actionMapping.findForward("success");
	}

	public ActionForward excluirUsuario(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse) throws Exception {
		System.out.println("UsuarioAction.excluirUsuario()");
		String idUsuario = httpServletRequest.getParameter("idUsuario");
		Usuario usuario = dao.retrieve(Long.parseLong(idUsuario));
		dao.delete(usuario);
		ActionMessages msgs = new ActionMessages();

		msgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("Exluido! "));
		this.saveMessages(httpServletRequest, msgs);
		return actionMapping.findForward("success");
	}
}
